package com.heima.jopo.web;

import com.heima.jopo.utilconn.GetConn;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DelectUser {

    public boolean delectUser(String id)throws Exception{

        Connection conn = GetConn.getConn();

        String sql="delete from t_user_info where id=?";

        try {
            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setInt(1,Integer.parseInt(id));

            int i = ps.executeUpdate();

            if (i>0){
                return true;
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new Exception("删除失败请查看");
        }
        return false;
    }


    public boolean delectUserinfo(String id){

        try {
            //获取mybatis核心配置文件  获得sqlSesssionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
                //获得SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession();

            int delete = sqlSession.delete("user.delectid",id);


            if (delete>0){
                return true;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
